﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour {
	public GameObject prefab;

	public Material red;
	public Material blue;

	private int num = 100;
	private float borad = 50f;
	private float cubeScale = 10f;
	private float randomAngle = 0.5f;
	private List<OBB> obbList = new List<OBB>();
	// Use this for initialization
	void Start () {
		for(int i = 0; i < num; i++)
        {
			GameObject go = Instantiate(prefab, transform);
			go.transform.position = new Vector3(Random.Range(-borad, borad), Random.Range(-borad, borad), 0);
			go.transform.localScale = new Vector3(Random.Range(1, cubeScale), Random.Range(1, cubeScale), 1);
            go.transform.localEulerAngles = new Vector3(0, 0, 0);
			OBB obb = new OBB(go);
			obb.addAngle = Random.Range(0.1f, randomAngle);
			obbList.Add(obb);
		}
	}
	
	// Update is called once per frame
	void Update () {
        for (int i = 0; i < obbList.Count; i++)
        {
            OBB obb = obbList[i];
            obb.go.transform.localEulerAngles = new Vector3(0, 0, obb.go.transform.localEulerAngles.z + obb.addAngle);
            obb.go.GetComponent<MeshRenderer>().material = blue;
            obb.UpdateGo();
        }

        for (int i = 0; i < obbList.Count - 1; i++)
        {
            OBB obb1 = obbList[i];
            for (int j = i + 1; j < obbList.Count; j++)
            {
                OBB obb2 = obbList[j];
                if (obb1.Check(obb2))
                {
                    obb1.go.GetComponent<MeshRenderer>().material = red;
                    obb2.go.GetComponent<MeshRenderer>().material = red;
                    break;
                }
            }
        }
    }
}
